/*
 * @Author: 葛文杰
 * @Date: 2021/1/29 15:46
 * @Description: egg proxy 数据源接口参数
 */
const { IS_NOT_DELETE, BASE_WHERE_CONDITION_SQL } = require('../util/common');

const TABLE_NAME = 'data_source_interface_param';

const TABLE_FIELD = 'id,dataSourceConfigId,interFaceParamType,interFaceParamName,interFaceParamValue';

module.exports = {


    async getInterfaceParamListByDataSourceConfigId(conn, { dataSourceConfigId, pageNo, pageSize }) {
        let start = (pageNo - 1) * pageSize;
        let selectSql = `select ${TABLE_FIELD} from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        conditionSql = `${conditionSql}`;
        if (dataSourceConfigId) {
            conditionSql = `${conditionSql} and dataSourceConfigId = ?`;
            params.push(dataSourceConfigId);
        }
        let sql = `${selectSql} ${conditionSql} limit ?,?`;
        params = [ ...params, start, pageSize ];
        let dataSourceFieldList = await conn.query(sql, params);
        return dataSourceFieldList;
    },

    /**
     * 获取数据源接口参数总数
     */
    async getInterfaceParamTotalByDataSourceConfigId(conn, { dataSourceConfigId }) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [ IS_NOT_DELETE ];
        if (dataSourceConfigId) {
            conditionSql = `${conditionSql} and dataSourceConfigId = ?`;
            params.push(dataSourceConfigId);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 获取数据源接口参数信息
     */
    async getInterfaceParamInfoById(conn, condition) {
        let result = await conn.get(TABLE_NAME, condition);
        return result;
    },

    /**
     * 新增数据源接口参数信息
     * @return {Promise<void>}
     */
    async insertInterfaceParamInfoById(conn, condition) {
        let result = await conn.insert(TABLE_NAME, condition);
        return result;
    },

    /**
     * 获取数据源接口参数信息
     * @return {Promise<void>}
     */
    async updateInterfaceParamInfoById(conn, condition) {
        let result = await conn.update(TABLE_NAME, condition);
        return result;
    },

    /**
     * 批量更新临时数据源id为真实id
     * @param conn
     * @param value
     * @param condition
     * @return {Promise<void>}
     */
    async updateDataSourceConfigTempIdById(conn, value, condition) {
        let result = await conn.update(TABLE_NAME, value, condition);
        return result;
    },


    /**
     * 删除数据源接口参数
     */
    async deleteInterfaceParamById(conn, condition) {
        let result = await conn.delete(TABLE_NAME, condition);
        return result;
    }
};
